package com.wu.foodsafetyplayer.widget;

import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.TextView;

import com.wu.foodsafetyplayer.utils.tv.TimeUtils;


/**
 * Like AnalogClock, but digital. Shows seconds.
 */
public class DateTextView2 extends TextView {
	private Context context;
	private Runnable mTicker;
	private Handler mHandler;

	public DateTextView2(Context context) {
		super(context);
		this.context = context;

	}

	public DateTextView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;

	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mHandler = new Handler();

		/**
		 * requests a tick on the next hard-second boundary
		 */
		mTicker = new Runnable() {
			public void run() {
				setText(TimeUtils
						.getCurrentTimeInString(TimeUtils.DATE_FORMAT_DATE2)
						+ "  " + TimeUtils.getWeek(context, null));
				long now = SystemClock.uptimeMillis();
				long next = now + (10000 - now % 10000);
				mHandler.postAtTime(mTicker, next);
			}
		};
		mTicker.run();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}

}
